package org.zxp.onedoteight.trywithresource;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @program: effectiveJava
 * @description: try(需要close的资源声明) 会自动调用close方法 TryWithResource
 * @author: X-Pacific zhang
 * @create: 2019-06-03 15:15
 **/
public class TryWithResourceTest {
    public static void main(String[] args) {
        File file = new File("output.txt");
        //这种写法会自动调用close方法 TryWithResource
        try(OutputStream os =  new FileOutputStream(file, true);) {
            String msg = "my name is zxp!";
            byte[] bytes = msg.getBytes();
            //写入文件
            os.write(bytes, 0, bytes.length);
            //清空管道
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
